package com.example.graduationdesignbackend.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.hutool.core.util.RandomUtil;
import com.example.graduationdesignbackend.constants.ResponseCode;
import com.example.graduationdesignbackend.dto.AdminLoginDto;
import com.example.graduationdesignbackend.dto.FeedbackDto;
import com.example.graduationdesignbackend.dto.PageDto;
import com.example.graduationdesignbackend.dto.UserLoginDto;
import com.example.graduationdesignbackend.entity.*;
import com.example.graduationdesignbackend.enums.InfoType;
import com.example.graduationdesignbackend.query.MerchantQuery;
import com.example.graduationdesignbackend.query.NewsQuery;
import com.example.graduationdesignbackend.query.ProductQuery;
import com.example.graduationdesignbackend.res.Res;
import com.example.graduationdesignbackend.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.File;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/public")
public class PublicController {
  @Autowired
  private IUserService userService;
  @Autowired
  private IFeedbackService feedbackService;
  @Autowired
  private IMerchantService merchantService;
  @Autowired
  private IInfoService infoService;
  @Autowired
  private INewsService newsService;
  @Autowired
  private IProductService productService;
  @Autowired
  private IAdminService adminService;
  @Autowired
  private IProductOrderService productOrderService;

  @PostMapping("/user/login")
  public Res<SaTokenInfo> login(@RequestBody UserLoginDto request) {
    SaTokenInfo loginToken = userService.login(request);
    return Res.yuze(loginToken);
  }

  @PostMapping("/user/loginPwd")
  public Res loginPwd(@RequestBody UserLoginDto userLoginDto){
    SaTokenInfo loginToken = userService.loginpwd(userLoginDto);
    return Res.yuze(loginToken);
  }
  @PostMapping("/admin/login")
  public Res login(@RequestBody AdminLoginDto adminLoginDto){
    SaTokenInfo loginToken = adminService.login(adminLoginDto);
    return Res.yuze(loginToken);
  }

  @PostMapping("/admin/loginPwd")
  public Res loginPwd(@RequestBody AdminLoginDto adminLoginDto){
    SaTokenInfo loginToken = adminService.loginpwd(adminLoginDto);
    return Res.yuze(loginToken);
  }

  @GetMapping("/getCheckCode")
  public Res<String>getLoginCode(String phone) {
    userService.getLoginCode(phone);
    String a = "成功发送验证码！";
    return Res.yuze(a);
  }

  @Value("${file.upload-path}")
  private String imgUrl;

  @ApiOperation(value = "返回前端发送来的图片")
  @PostMapping("/upload")
  public Res<String> returnImg(@RequestBody MultipartFile file){
    String originalFilename = file.getOriginalFilename();
    int index = originalFilename.indexOf(".");
    String formatFileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))+RandomUtil.randomNumbers(9);
    String newFileName = formatFileName + originalFilename.substring(index);
    try {
      file.transferTo(new File(imgUrl + newFileName));
      return Res.yuze(newFileName);
    }catch (Exception e){
      e.printStackTrace();
    }
    return Res.fail(ResponseCode.Fail);
  }

  @GetMapping("/getImage/{imageName}")
  public Res<String> getImage(@PathVariable("imageName") String imageName){
    Path imagePath = Paths.get(imgUrl, imageName);
    URI imageUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
            .path("/images/")
            .path(imagePath.getFileName().toString())
            .build()
            .toUri();
    return Res.yuze(imageUrl.toString());
  }

  @PostMapping("/feedback")
  public Res create(@RequestBody FeedbackDto feedback){
    return Res.yuze(feedbackService.createFeedback(feedback));
  }

  @GetMapping("/info")
  public Res<Info> getInfo(InfoType type){
    return Res.yuze(infoService.getInfo(type));
  }

  @GetMapping("/merchant")
  public Res<Merchant> getMerchantInfo(String id){
    return Res.yuze(merchantService.getById(id));
  }

  @GetMapping("/merchant/page")
  public Res<PageDto<Merchant>> getMerchantPages(MerchantQuery merchantQuery){
    return Res.yuze(merchantService.getMerchantPage(merchantQuery));
  }

  @GetMapping("/merchant/list")
  public Res<List<Merchant>> getMerchantList(){
    return Res.yuze(merchantService.getMerchantList());
  }

  @GetMapping("/news")
  public Res getNewsInfo(String id){
    News news = newsService.getById(id);
    return Res.yuze(news);
  }

  @GetMapping("/news/page")
  public Res<PageDto<News>> getNewsPages(NewsQuery newsQuery){
    return Res.yuze(newsService.getNewsPages(newsQuery));
  }

  @GetMapping("/product")
  Res<Product> getProductInfo(String id){
    return Res.yuze(productService.getById(id));
  }

  @GetMapping("/product/page")
  Res<PageDto<Product>> getProductPage(ProductQuery productQuery){
    return Res.yuze(productService.getProductPage(productQuery));
  }

  @GetMapping("/sale")
  public Res getSale(){
    return productOrderService.getSalesVolume();
  }

  @PostMapping("/createProductOrder")
  public Res createProductOrder(@RequestBody ProductOrder productOrder){
    productOrderService.createProductOrder(productOrder);
    return Res.yuze();
  }

}
